﻿namespace CompressTables;
using CompressShaders;
using System.IO;
using System.Runtime.CompilerServices;

/// <summary>Utility app to compress lookup tables data with LZ4-HC, and generate C++ source with the data</summary>
internal class Program
{
	static string getSolutionRoot( [CallerFilePath] string? path = null )
	{
		string? dir = Path.GetDirectoryName( path );
		dir = Path.GetDirectoryName( dir );
		dir = Path.GetDirectoryName( dir );
		return dir ?? throw new ApplicationException();
	}

	static void writeArray( byte[] compressed, string path )
	{
		using var stream = File.CreateText( path );
		stream.WriteLine( "// This source file is generated by a tool" );
		stream.Write( @"static const std::array<uint8_t, {0}> s_tableData = {{", compressed.Length );

		for( int i = 0; i < compressed.Length; i++ )
		{
			if( 0 == i % 32 )
				stream.Write( "\r\n\t" );
			else
				stream.Write( ' ' );
			stream.Write( "0x{0:X02},", compressed[ i ] );
		}
		stream.Write( @"
};" );
	}

	static void Main( string[] args )
	{
		byte[] source = File.ReadAllBytes( @"C:\Temp\2remove\Whisper\tables.bin" );
		byte[] result = LZ4.compressBuffer( source );

		string root = getSolutionRoot();
		string path = Path.Combine( root, "Whisper", "ML", "LookupTablesData.inl" );
		writeArray( result, path );
	}
}